/*
 ============================================================================
 Name		: ListWidget.h
 Author	  : Zhang Jiawei
 Version	 : 1.0
 Copyright   : Your copyright notice
 Description : CListWidget declaration
 ============================================================================
 */

#ifndef LISTWIDGET_H
#define LISTWIDGET_H

// INCLUDES
#include "Control.h"
#include "Flickable.h"
#include "ScrollBar.h"
#include "MWidgetObserver.h"
#include <GDI.H>

// CLASS DECLARATION
class TWidgetItem
{
public:
    enum TAlignment
    {
        EHLeft,
        EHRight,
        EHCenter
    };
    
    TWidgetItem();
    TWidgetItem(const TWidgetItem& aItem);
    TWidgetItem& operator=(const TWidgetItem& aItem);
    
    void SetText(const TDesC& aText);
    void SetText(const TDesC8& aText);
    void SetColor(TRgb aColor);
    void SetAlignment(TAlignment aAlignment);
    
    CDesCArray* GetMultiLineText();
    
    const TDesC& GetText() const;
    TRgb GetColor() const;
    TAlignment GetAlignment() const;
    
private:
    TBuf<64> iText;
    TRgb iColor;
    TAlignment iAlignment;
    
    CDesCArray*	iTextArray;
};

/*class MWidgetObserver
{
public:
    virtual void UpdateScreen() = 0;
    virtual void SelectOneRow(TInt aRowIndex) = 0;
    virtual void LoseFocus(const TInt& keycode) = 0;
};*/

class CListWidget: public CControl, public MFlickableCallback, public MScrollBarObserver
{
public:
    ~CListWidget();
    static CListWidget* NewL(MWidgetObserver* aObserver = NULL);
    static CListWidget* NewLC(MWidgetObserver* aObserver = NULL);

protected:
    CListWidget();
    void ConstructL(MWidgetObserver* aObserver = NULL);
    
public:
    // form CControl
    virtual void Draw(CFbsBitGc& g);
    virtual TBool OnKeyDown(const TInt& keycode);
    virtual TBool OnMouseDown(const TPoint& aPt);
    virtual TBool OnMouseMove(const TPoint& aPt);
    virtual TBool OnMouseUp(const TPoint& aPt);
    virtual void SetRect(const TInt& left,const TInt& top,const TInt& right, const TInt& bottom);
    virtual void FocusChanged(TBool aFocus, const TInt keyCode = 0);
    
    // from MFlickableCallback
    virtual TPoint ScrollOffset() const;
    virtual void SetScrollOffset(const TPoint& aOffset);
    
    // from MScrollBarObserver
    virtual void ChangeScrollPos(TInt nPos);
    
public:
    // new functions
    void SetRowCount(TInt aRowCount);
    TInt RowCount() const;
    void SetHighlightColor(TRgb aHighlightColor);
    TRgb HighlightColor() const;
    void Clear();
    void AddItem(TWidgetItem& item);    
    const TDesC& GetCurrentItemText();
    void SetLoop(TBool aLoop);
    
protected:
	virtual void SetFont(CFont* aFont);
	virtual void DrawItem(CFbsBitGc& g, TWidgetItem& aItem, TRect aRect);
	virtual void SetScrollBar(TInt nPos, TInt nRange);

	virtual TInt CalcMaxRowWidth();

	TWidgetItem& GetItem(TInt aRow);
    
protected:
    MWidgetObserver* iObserver;
    RArray<TWidgetItem> iItemArray;
    TInt iRowCount;
    TInt iMargin;
    CFont* iFont;
    TRgb iHighlightColor;
    
    CFlickable* iFlickable;
    TPoint iOffset;
    TInt iHeight;
    TInt iHighlight;
//    TInt iOrignHighlight;
    TInt iStartRow;
    TInt iEndRow;
    
    CScrollBar* iScrollBar;
    TBool iShowScroll;
    TBool iIsLoop;
};

inline void TWidgetItem::SetText(const TDesC& aText)
{
    iText.Copy(aText);
}

inline void TWidgetItem::SetColor(TRgb aColor)
{
    iColor = aColor;
}

inline void TWidgetItem::SetAlignment(TAlignment aAlignment)
{
    iAlignment =aAlignment;
}

inline const TDesC& TWidgetItem::GetText() const
{
    return iText;
}

inline TRgb TWidgetItem::GetColor() const
{
    return iColor;
}

inline TWidgetItem::TAlignment TWidgetItem::GetAlignment() const
{
    return iAlignment;
}

inline TInt CListWidget::RowCount() const
{
    return iRowCount;
}

inline void CListWidget::SetHighlightColor(TRgb aHighlightColor)
{
    iHighlightColor = aHighlightColor;
}

inline TRgb CListWidget::HighlightColor() const
{
    return iHighlightColor;
}

inline void CListWidget::SetLoop(TBool aLoop)
{
    iIsLoop = aLoop;
}

#endif // LISTWIDGET_H
